package com.caiyigo.baihua.serviceImpl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.caiyigo.baihua.Const.Dictionary;
import com.caiyigo.baihua.dao.KaoshicjMapper;
import com.caiyigo.baihua.dao.KaoshicjMapperSql;
import com.caiyigo.baihua.dto.CjBean;
import com.caiyigo.baihua.dto.KaoshicjDto;
import com.caiyigo.baihua.model.Kaoshicj;
import com.caiyigo.baihua.model.Xuesheng;
import com.caiyigo.baihua.service.KaoshicjService;
import com.caiyigo.baihua.utils.StringUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

@Transactional
@Service
public class KaoshicjServiceImpl implements KaoshicjService {
	@Resource(name = "kaoshicjMapper")
	KaoshicjMapper kaoshicjMapper;
	@Resource(name = "kaoshicjMapperSql")
	KaoshicjMapperSql kaoshicjMapperSql;

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int deleteByPrimaryKey(Integer ksId) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.deleteByPrimaryKey(ksId);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int insert(Kaoshicj record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.insert(record);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int insertSelective(Kaoshicj record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.insertSelective(record);
	}

	@Override
	public Kaoshicj selectByPrimaryKey(Integer ksId) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.selectByPrimaryKey(ksId);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int updateByPrimaryKeySelective(Kaoshicj record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.updateByPrimaryKeySelective(record);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int updateByPrimaryKey(Kaoshicj record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.updateByPrimaryKey(record);
	}

	@Override
	public List getList(KaoshicjDto dto, PageBounds pageBounds) {
		// TODO Auto-generated method stub
		return kaoshicjMapperSql.getList(dto, pageBounds);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int batchDelete(String[] ids) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.batchDelete(ids);
	}

	@Override
	public List<KaoshicjDto> getAll() {
		// TODO Auto-generated method stub
		return kaoshicjMapper.getAll();
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int batchUpdate(List<KaoshicjDto> record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.batchUpdate(record);
	}

	@Override
	public List<KaoshicjDto> getAllCjByZyidAndXsid(Xuesheng xuesheng) {
		// TODO Auto-generated method stub
		List<KaoshicjDto> dtos = kaoshicjMapper.getAllCjByZyidAndXsid(xuesheng);
		List<KaoshicjDto> removeResult = new ArrayList<KaoshicjDto>();
		removeResult.addAll(dtos);
		Map<String, KaoshicjDto> result = new LinkedHashMap<String, KaoshicjDto>();
		for (KaoshicjDto kaoshicjDto : dtos) {
			if (!result.containsKey(kaoshicjDto.getXsid()
					+ kaoshicjDto.getKmid())) {
				result.put(kaoshicjDto.getXsid() + "-" + kaoshicjDto.getKmid(),
						kaoshicjDto);
			}

		}
		for (KaoshicjDto kaoshicjDto : removeResult) {

			String key = kaoshicjDto.getXsid() + "-" + kaoshicjDto.getKmid();

			if (!StringUtils.isNumeric(kaoshicjDto.getCjfs())) {
				kaoshicjDto.setCjfs("0");
			}
			if (!StringUtils.isNumeric(result.get(key).getCjfs())) {
				result.get(key).setCjfs("0");
			}
			if (Integer.valueOf(kaoshicjDto.getCjfs()) > Integer.valueOf(result
					.get(key).getCjfs())) {

				if (org.apache.commons.lang3.StringUtils.isNotEmpty(result.get(
						key).getHistoryCj())) {
					result.get(key).setHistoryCj(
							result.get(key).getHistoryCj() + ";"
									+ result.get(key).getCjfs());
				} else {
					result.get(key).setHistoryCj(result.get(key).getCjfs());
				}

				result.get(key).setCjfs(kaoshicjDto.getCjfs());

			} else if (Integer.valueOf(kaoshicjDto.getCjfs()) < Integer
					.valueOf(result.get(key).getCjfs())) {
				if (org.apache.commons.lang3.StringUtils.isNotEmpty(result.get(
						key).getHistoryCj())) {
					result.get(key).setHistoryCj(
							result.get(key).getHistoryCj() + ";"
									+ kaoshicjDto.getCjfs());
				} else {
					result.get(key).setHistoryCj(kaoshicjDto.getCjfs());
				}
			}
		}
		List<KaoshicjDto> r = new ArrayList<KaoshicjDto>();
		for (String kString : result.keySet()) {
			if (org.apache.commons.lang3.StringUtils.isEmpty(result
					.get(kString).getHistoryCj())) {
				result.get(kString).setHistoryCj("");
			}
			if (org.apache.commons.lang3.StringUtils.isEmpty(result
					.get(kString).getCjfs())) {
				result.get(kString).setCjfs("");
			}
			if (result.get(kString).getCjfs().equals("0")) {
				result.get(kString).setCjfs("");
			}
			result.get(kString).setKmlxmc(
					Dictionary.KMLX.getName(result.get(kString).getKmlx()));
			r.add(result.get(kString));
		}
		return r;
	}

	@Override
	public List<CjBean> smsCjList(String pici) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.smsCjList(pici);
	}

	@Override
	public int updateSmsCjList(KaoshicjDto record) {
		// TODO Auto-generated method stub
		return kaoshicjMapper.updateSmsCjList(record);
	}

}
